package com.example.logistics_manage.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.logistics_manage.pojo.User;
import com.example.logistics_manage.pojo.Vehicle;
import com.example.logistics_manage.result.restful.R;
import com.example.logistics_manage.result.restful.ResponseEunm;
import com.example.logistics_manage.service.IVehicleService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/vehicle")
@CrossOrigin
public class VehicleController {
    @Autowired
    IVehicleService vehicleService;
    /**
     * 分页查询所有车辆
     * @param current 页码
     * @param size 每页大小
     * @return 页面
     */
    @GetMapping("/findAllVehicle")
    public R<IPage<Vehicle>> findAllVehicle(@RequestParam("current") Integer current, @RequestParam("size") Integer size){
      return new R<>(ResponseEunm.SUCCESS,vehicleService.findAllVehicle(current,size));
    }
    /**
     * id查询
     * @param vehicleId id
     * @return 对象
     */
    @GetMapping("/findVehicleById/{vehicleId}")
    public R<Vehicle> findVehicleById(@PathVariable("vehicleId") Integer vehicleId){
       return new R<>(ResponseEunm.SUCCESS,vehicleService.findVehicleById(vehicleId));
    }
    /**
     * 通过id更新
     * @param vehicle 对象
     */
    @PutMapping("/updateVehicleById")
    public R<Vehicle> updateVehicleById(@RequestBody Vehicle vehicle){
        vehicleService.updateVehicleById(vehicle);
        return new R<>(ResponseEunm.SUCCESS,null);
    }

    /**
     * 删除车辆
     * @param vehicle 车
     * @return c
     */
    @DeleteMapping("/findVehicleById/{vehicleId}")
    public R<Vehicle> deleteVehicleById(@PathVariable("vehicleId")Integer vehicle){
        if(vehicleService.deleteVehicleById(vehicle)){
            return new R<>(ResponseEunm.SUCCESS,null);
        }else {
            return new R<>(ResponseEunm.DELETE_FAIL,null);
        }
    }
    /**
     * 添加数据进入审核
     * @param vehicle 车辆
     * @return 0
     */
    @PostMapping("/addVehicleFirst")
    public R<Vehicle> addVehicleFirst(@RequestBody Vehicle vehicle){
        if (vehicleService.addVehicleFirst(vehicle)) {
            return new R<>(ResponseEunm.SUCCESS, null);
        }else {
            return new R<>(ResponseEunm.ADD_FAIL,null);
        }
    }

    /**
     * 条件查询数据
     * @param vehicle 条件
     * @param current 1
     * @param size 1
     * @return 1
     */
    @PostMapping("/findByData")
    public R<IPage<Vehicle>> findByData(@RequestBody Vehicle vehicle,@RequestParam("current") Integer current,@RequestParam("size")Integer size){
        return new R<>(ResponseEunm.SUCCESS,vehicleService.findByData(vehicle,current,size));
    }

    /**
     * 查询待审批的数据
     * @return 数据
     */
    @GetMapping("/findReadyExamData")
    public R<List<Vehicle>> findReadyExamData(){
        return new R<>(ResponseEunm.SUCCESS,vehicleService.findReadyExamData());
    }

    /**
     * 审批车辆通过
     * @param user q
     * @return q
     */
    @PutMapping("/passExam")
    public R<Vehicle> passExam(@RequestBody User user,@RequestParam("vehicleId")Integer vehicleId){
        if (vehicleService.passExam(user,vehicleId)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.UPDATE_FAIL,null);
    }

    /**
     * 审批车辆未通过删除记录
     * @param vehicleId id
     * @return null
     */
    @DeleteMapping("/failExam/{vehicleId}")
    public R<Vehicle> failExam(@PathVariable("vehicleId") Integer vehicleId){
        if (vehicleService.failExam(vehicleId)==1){
            return new R<>(ResponseEunm.SUCCESS,null);
        }
        return new R<>(ResponseEunm.DELETE_FAIL,null);
    }
}
